package david.java.tools;


import org.apache.commons.lang3.StringUtils;

import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * @Description:
 * @Author: ZhaoDawei
 * @Date: Create in 3:43 下午 2023/5/17
 */
public class DeDuplicate extends BaseUtil {

     static String input = "src/tmp/de_duplicate";
     static String output_v = "target-tmp/de_duplicate-vertical";
     static String output_h = "target-tmp/de_duplicate-horizontal";


    public static void main(String[] args) throws IOException {

        mkdirIfNotExists(output_h);
        mkdirIfNotExists(output_v);
        execute(input, output_h, true);
        execute(input, output_v, false);

    }


    private static void execute(String input, String output, Boolean horizontal) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(input));
        BufferedWriter bw = new BufferedWriter(new FileWriter(output));
        Set<String> result = new HashSet<>();
        String line;
        while ((line = br.readLine()) != null) {
            if (StringUtils.isNotBlank(line)) {
                line = line.trim();
                if (!line.startsWith("--")) {
                    result.add(line);
                }
            }
        }

        ArrayList<String> list = new ArrayList<>(result);
        Collections.sort(list);

        if (horizontal) {
            bw.write(String.join(",", list));
        } else {
            for (int i = 0; i < list.size(); i++) {
                if (i != 0) {
                    bw.newLine();
                }
                bw.write(list.get(i));
            }
        }

        bw.flush();
        br.close();
        bw.close();
    }

}
